// pages/login/login.js
const request = require("../../utils/request.js");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    no: "",
    pwd: "",
    noinput: false,
    pwdinput: false,
    loginMessage:true,
  },
  noinput: function(e) {
    this.setData({ no: e.detail.value });
    this.setData({ noinput: true });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({ disabled: false });
    }
  },
  pwdinput: function(e) {
    this.setData({ pwd: e.detail.value });
    this.setData({ pwdinput: true });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({ disabled: false });
    }
  },
  formSubmit: function(e) {
    var params = {
      username: e.detail.value.no,
      password: e.detail.value.pwd
    };
    wx.setStorage({
      key: 'username',
      data: e.detail.value.no,
    })
    wx.setStorage({
      key: 'password',
      data: e.detail.value.pwd,
    })


    app.request.requestPostApi(
      app.globalData.huodiepath2 + "applet/live.php?op=GetPushLiveInfo",
      params,
      this,
      this.successFun,
      this.failFun
    );
    wx.showLoading({
      title: "登录中..."
    });
    // console.log(e);
    this.setData({ disabled: true });
  },
 

  failFun: function(res, selfObj) {

    console.log(res,selfObj)

    wx.setStorage({
      key: 'username',
      data: "",
    })
    wx.setStorage({
      key: 'password',
      data: "",
    })
    wx.showToast({
      title: "服务器出现错误",
      icon: "none",
      duration: 2000
    });
  },
  successFun: function(res, selfObj) {
    console.log('successFun',res);
    if (res.status==undefined){
      wx.showToast({
        title: "账号密码错误",
        icon: "none",
        duration: 2000
      });
   return false;
    }
    if (res.status==0) {
      wx.showToast({
        title: res.msg,
        icon: "none",
        duration: 2000
      });
    } else {
      wx.setStorageSync("live", res);
      wx.showToast({
        title: res.msg,
        icon: "success",
        duration: 2000
      });
      setTimeout(function() {
      wx.navigateTo({
       url:'../home-page/main'
      })
     , 2000});
  }
},
  getuserinfo(e){
    console.log(e)
    if (e.detail.userInfo){
      this.setData({
        loginMessage:false
      })
    }else{
      this.setData({
        loginMessage: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var no = wx.getStorageSync("username");
    var pwd = wx.getStorageSync("password");

    console.log('缓存中账号密码',no,pwd)

    this.setData({
      no:no,
      pwd:pwd
    })
    wx.getUserInfo({
      success(res){
        console.log('userInfo',res)
        that.setData({
          loginMessage:false
        })
      },
      fail(res){
        that.setData({
          loginMessage:true
        })
      }
    })
    this.setData({ disabled: false });
    var student = wx.getStorageSync("student");
    if (
      typeof student == "object" &&
      student.no != "" &&
      student.classid != ""
    ) {
      wx.switchTab({
        url: "../teacher/teacher"
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.no == "" || this.data.pwd == "") {
      this.setData({ disabled: true });
    } else {
      this.setData({ disabled: false });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: "pages/home-login/main",
      imageUrl:
        "https://res.lexiangpingou.cn/images/applet/hdassistant_bg.png"
    };
  },
  goHDY(){
    let url = "https://www.lexiangpingou.cn/"

    wx.navigateTo({
      url: '../webwiew/webwiew?url='+encodeURIComponent(url),
    })
  }

});
